from flask import Blueprint,g
from model.model import *

test = Blueprint('test',__name__,url_prefix='/test')

@test.route('/zql',methods=['get'])
def zql():
    datas = Goods.query.all()
    data_list = [i.__dict__ for i in datas]
    g.id = 1
    # 循环思路
    # for i in data_list:
    #     for ret_i in ret:
    #         if i['cate_id'] == ret_i['id']:
    #             if ret_i.get('son'):
    #                 ret_i['son'].append(i)
    #                 break
    #             else:
    #                 ret_i['son'] = [i]
    #                 break
    #         else:
    #             ret.append(i)
    ret_data = []
    ret = []
    for i in data_list:
        i.pop('_sa_instance_state')
        new_ret = add_som(ret_data,item=i)
        if new_ret == new_ret:
            ret_data.append(i)
    return ret_data

def add_som(ret_data,item):
    for i in ret_data:
       if item['cate_id'] == ['id']:
          i['son'] =  i.get('son',[]) + [item]
          return True

       if 'son' in i:
           return add_som(i['son'],item)